//
//  SexViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/8/29.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "SexViewController.h"
#import "RentVieitor.h"

@interface SexViewController ()

@end

@implementation SexViewController  {
    
    NSString *sex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"性别";
    sex = @"1";
    self.view.backgroundColor = BackgroundColor;
    [self creatLeftView];
    [self creatView];
    
    
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 80,15 );
    [leftBtn setImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn setTitle:@"个人信息" forState:UIControlStateNormal];
    leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftBtn setTitleColor:[Utils colorConvertFromString:@"#333333"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 自定义导航栏左侧按钮
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.frame = CGRectMake(0, 0, 50, 20);
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[Utils colorConvertFromString:@"#333333"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)creatView {
    
    NSArray *array = @[@"男",@"女"];
    for (int i=0; i<array.count; i++) {
        
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 10+50*i, KWidth, 50)];
        whiteView.backgroundColor = [UIColor whiteColor];
        whiteView.userInteractionEnabled = YES;
        whiteView.tag = 100+i;
        [self.view addSubview:whiteView];
        //添加手势
        UITapGestureRecognizer * PrivateLetterTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAvatarView:)];
        PrivateLetterTap.numberOfTouchesRequired = 1; //手指数
        PrivateLetterTap.numberOfTapsRequired = 1; //tap次数
        [whiteView addGestureRecognizer:PrivateLetterTap];

        UILabel *boyLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 50, 30)];
        boyLabel.text = array[i];
        boyLabel.textAlignment = NSTextAlignmentLeft;
        boyLabel.font = [UIFont systemFontOfSize:15];
        boyLabel.textColor = [Utils colorConvertFromString:@"#101010"];
        [whiteView addSubview:boyLabel];
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(KWidth-14-30, (50-11)/2, 14, 11)];
        imgView.tag = 200+i;
        imgView.image = [UIImage imageNamed:@"sexSelect"];
        if ([self.getsex isEqualToString:@"2"]) {
            if (i==0) {
                imgView.hidden = YES;
            }
        }else {
            
            if (i==1) {
                imgView.hidden = YES;
            }
        }
        
        [whiteView addSubview:imgView];
        
        if (i==0) {
            //分割线
            CALayer *MlineLayer = [CALayer layer];
            MlineLayer.backgroundColor = [[Utils colorConvertFromString:@"#cfcfcf"] CGColor];
            MlineLayer.frame = CGRectMake(20 ,49, KWidth-40, 1);
            [whiteView.layer addSublayer:MlineLayer];
        }
        
        }
}

- (void)tapAvatarView:(UIGestureRecognizer *)recognizer {
    
    UIView *view = (UIView *)[self.view viewWithTag:100];
    UIImageView *imgView = (UIImageView *)[view viewWithTag:200];
    
    UIView *view2 = (UIView *)[self.view viewWithTag:101];
    UIImageView *imgView2 = (UIImageView *)[view2 viewWithTag:201];
    
    if (recognizer.view.tag == 100) {
        
        imgView.hidden = NO;
        imgView2.hidden = YES;
        sex = @"1";
    
    }else {
        
        imgView2.hidden = NO;
        imgView.hidden = YES;
        sex = @"2";
    }
}

//保存
- (void)saveAction {
    
    NSDictionary *dataDic = User_listsValue;
    NSString *url = [NSString stringWithFormat:@"%@member/info/sex",Main_Url];
    NSString *number = dataDic[@"number"];
    NSString *token = dataDic[@"token"];
    NSString *md5String = [NSString stringWithFormat:@"%@%@%@%@%@",number,sex,token,APPKEY_URL,url];
   
    //32位小写
    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
    NSDictionary *param = @{
                            @"number":number,
                            @"sex":sex,
                            @"token":token,
                            @"sign":md5String
                            };
    
    [AFHttpTool requestWihtUrl:url params:param success:^(id response) {
        
        
        if ([response[@"code"] isEqualToString:@"10000"]) {
            
            [APPDELEGATE showEditSuccess:@"修改成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"update" object:nil userInfo:nil];
        }
        
        
    } failure:^(NSError *err) {
        
        
    }];
    

    
}

- (void)backAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
